using System;
using System.Collections.Generic;
using System.Text;

namespace SubversionReports.Entities
{
    public class Revision
    {
        #region fields

        private int id;
        private string author;
        private DateTime date;
        private List<Path> paths;
        private string comment;
        private string summary;

        #endregion

        #region accessors

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// Gets or sets the author.
        /// </summary>
        /// <value>The author.</value>
        public string Author
        {
            get { return author; }
            set { author = value; }
        }

        /// <summary>
        /// Gets or sets the date.
        /// </summary>
        /// <value>The date.</value>
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }

        /// <summary>
        /// Gets or sets the paths.
        /// </summary>
        /// <value>The paths.</value>
        public List<Path> Paths
        {
            get { return paths; }
            set { paths = value; }
        }

        /// <summary>
        /// Gets or sets the comment.
        /// </summary>
        /// <value>The comment.</value>
        public string Comment
        {
            get { return comment; }
            set { comment = value; }
        }

        /// <summary>
        /// Gets the revision summary.
        /// </summary>
        /// <value>The summary.</value>
        public string Summary
        {
            get
            {
                if(string.IsNullOrEmpty(summary))
                {
                    StringBuilder stringBuilder = new StringBuilder(string.Format("{0} Change{1}:", Paths.Count, (Paths.Count > 1) ? "s" : string.Empty));
                    int modified = 0, added = 0, deleted = 0;
                    Paths.ForEach(delegate(Path path) { switch (path.RevisionType) { case PathRevisionType.Modified: modified++; break; case PathRevisionType.Added: added++; break; case PathRevisionType.Deleted: deleted++; break; } });
                    if (modified > 0)
                        stringBuilder.AppendFormat("{0} {1} file{2} modified", (stringBuilder.ToString().EndsWith(":") ? string.Empty : ","), modified, (modified > 1) ? "s" : string.Empty);
                    if (added > 0)
                        stringBuilder.AppendFormat("{0} {1} file{2} added", (stringBuilder.ToString().EndsWith(":") ? string.Empty : ","), added, (added > 1) ? "s" : string.Empty);
                    if (deleted > 0)
                        stringBuilder.AppendFormat("{0} {1} file{2} deleted", (stringBuilder.ToString().EndsWith(":") ? string.Empty : ","), deleted, (deleted > 1) ? "s" : string.Empty);
                    summary = stringBuilder.ToString();
                }
                return summary;
            }
        }

        #endregion

        #region constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Revision"/> class.
        /// </summary>
        public Revision ()
        {
            
        }

        #endregion
    }
}
